﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Threading.Tasks;


namespace GPS.Controllers.GPS_TheoDoiBienDong
{
    public class GPS_BienDongPhuongTienController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        #region Index
        public ActionResult Index(ViewModelDPT SearchModel, int? currentPage)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, SearchModel.pPT_DVGTVT_MA);
            SearchModel.pPT_DVGTVT_MA = getdvgtvt_Ma;
            TempData["DVGTVT_MA"] = getdvgtvt_Ma;

            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<DPT>(o => (SearchModel.pPT_BIENSO == null || o.PT_BIENSO.ToUpper().Contains(SearchModel.pPT_BIENSO.ToUpper().Trim()))
                                                && (SearchModel.pPT_SODK == null || o.PT_SODK.ToUpper().Contains(SearchModel.pPT_SODK.ToUpper().Trim()))
                                                && (SearchModel.pPT_DVGTVT_MA == null || o.PT_DVGTVT_MA == SearchModel.pPT_DVGTVT_MA)
                                                && (SearchModel.pPT_ACTIVE == null || o.PT_ACTIVE == SearchModel.pPT_ACTIVE)
                                                );
            ViewBag.TotalRow = entities.Count().ToString();

            var model = new List<DPT>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<DPT>(currentEntity));
            }

            Int16 icount = 1;
            foreach (var item in model)
            {
                item.STT = icount;
                icount++;
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }
        public void initialCategorySearchAction(ViewModelDPT viewModel)
        {
            ViewBag.pPT_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            viewModel.lstDonViGTVT = _db.GetAll<SDONVIGTVT>();

        }
        #endregion

        #region Detail
        public ActionResult Detail(string id1, string id2)
        {
            CrudModelDPT _model = new CrudModelDPT();
            DPT model = _db.GetOne<DPT>(o => o.PT_BIENSO == id1 && o.PT_DVGTVT_MA == id2);
            GPS.COMMON.Helpers.CopyObject<CrudModelDPT>(model, ref _model);

            initialCategoryDetailAction(_model);
            return PartialView("_Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelDPT view)
        {
            if (view.PT_ACTIVE == "1")
            {
                ViewBag.txtTrangThai = "Hiệu Lực";
            }
            else
            {
                ViewBag.txtTrangThai = "Không Hiệu Lực";
            }
            ViewBag.PT_DVGTVT_MAss = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"
                                    && o.DVGTVT_MA == view.PT_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
        }
        #endregion

        #region"Biến động Phương tiện"
        public ActionResult Edit(string id1, string id2)
        {
            string _BIENSO = id1;
            string _GV_DVGTVT_MA = id2;
            _BIENSO = _BIENSO.Replace("%", ":");
            DPT model = _db.Filter<DPT>(x => x.PT_DVGTVT_MA == id2 && x.PT_BIENSO == _BIENSO).FirstOrDefault();
            CrudModelDLSPT _model = new CrudModelDLSPT();
            _model.LSPT_BIENSO = model.PT_BIENSO;
            _model.LSPT_SOGPXTL_CU = model.PT_SOGPXTL;
            _model.LSPT_NGAYGPXTL_CU = model.PT_NGAYGPXTL;
            _model.LSPT_NGAYHHGPXTL_CU = model.PT_NGAYHHGPXTL;
            _model.LSPT_DVGTVT_MA_CU = model.PT_DVGTVT_MA;
            ViewBag.DVGTVT_TEN = _db.GetOne<SDONVIGTVT>(x => x.DVGTVT_MA == model.PT_DVGTVT_MA && x.DVGTVT_MAQL == "40").DVGTVT_TEN;
            ViewBag.PT_ANHXE = model.PT_ANHXE;
            ViewBag.DVGTVT_MA_MOIs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA != _GV_DVGTVT_MA), "DVGTVT_MA", "DVGTVT_TEN");
            return PartialView("_Edit", _model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDLSPT model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                DLSPT _model = new DLSPT();
                GPS.COMMON.Helpers.CopyObject<DLSPT>(model, ref _model);
                _model.LSPT_NGAYNHAP = DateTime.Now;
                _model.LSPT_NGUOINHAP = "admin";
                if (_model.LSPT_STATUS != "1")
                    _model.LSPT_DVGTVT_MA_MOI = null;
                _model.ID = EFSequence.GetNextValue("SEQ_DLSPT_ID");
                _db.Insert<DLSPT>(_model);
                //update trạng thái DGV:
                string _id1 = model.LSPT_BIENSO;
                string _id2 = model.LSPT_DVGTVT_MA_CU;
                var _dpt = _db.GetOne<DPT>(o => o.PT_BIENSO == _id1 && o.PT_DVGTVT_MA == _id2);
                _dpt.PT_ACTIVE = model.LSPT_STATUS;
                _db.Update<DPT>(_dpt);
                string url = Url.Action("Index", "GPS_BienDongPhuongTien");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Edit", model);
            }
        }

        #endregion


    }
}